﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		
		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		
		response = structNew();
		structInsert(response, "success", false, true);
		
		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		transaction {
			
			/* 载入教学任务 */
			switch(taskType){
					case 'C':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_common_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'P':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'R':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_reopen_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'T':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_sport_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
				}
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				rs_task = queryObj.execute(sql = sql).getResult();
			
			/* 载入排课任务 */
			
			/* 删除教学班时间占用情况表 */ 
			sql = "	DELETE FROM
							t_task_timetable ttt
						WHERE
							ttt.task_id = :taskId ";
				
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			/* 删除地点时间占用表 */
			sql = "	DELETE FROM
							t_clsroom_timetable ttt
						WHERE
							ttt.crtime_id IN(
								SELECT
									tf.crtime_id
								FROM
									t_clsroom_forcourse tf
								WHERE
									tf.task_id = :taskId 
							)";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);	
			/* 删除教师时间占用表 */
			sql = "	DELETE FROM
							t_teacher_timetable ttt
						WHERE
							ttt.task_id = :taskId ";
				
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
					
			/* 解锁排课任务 */
			taskProp.parseProperty( rs_task.task_prop );
			taskProp.setArrangeDone( false );
					
				
			/* 清除排课时间 地点 */
				
			/* 重置教学班最大容量 */
			switch(taskType){
				case 'C':
					sql = "	UPDATE
								t_common_task t
							SET
								t.task_prop = :taskProp ,
								t.task_time = '' ,
								t.task_place = '' ,
								t.task_capacity = 0
							WHERE
								t.tsk_id = :taskId ";
					break;
				case 'P':
					sql = "	UPDATE
								t_task t
							SET
								t.task_prop = :taskProp ,
								t.task_time = '' ,
								t.task_place = '' ,
								t.task_capacity = 0
							WHERE
								t.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "	UPDATE
								t_reopen_task t
							SET
								t.task_prop = :taskProp ,
								t.task_time = '' ,
								t.task_place = '' ,
								t.task_capacity = 0
							WHERE
								t.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "	UPDATE
								t_sport_task t
							SET
								t.task_prop = :taskProp ,
								t.task_time = '' ,
								t.task_place = '' ,
								t.task_capacity = 0
							WHERE
								t.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="taskProp", value=taskProp.stringProperty(), cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
				
			/* 清除已排周学时 */
			
			sql = "	UPDATE
							t_task_arrange t
						SET
							t.period_done = 0
						WHERE
							t.task_id = :taskId ";
				
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			/* 载入调停课信息 */
			sql = "	DELETE FROM
							t_task_adjust t
						WHERE
							t.task_id = :taskId ";
				
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			structInsert(response, "success", true, true);
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>